package com.atzzy.ysmall.ysmall_productserviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atzzy.ysmall.ysmall_allService.ProductService.ProService;
import com.atzzy.ysmall.ysmall_beans.*;
import com.atzzy.ysmall.ysmall_common.utils.PageHelper;
import com.atzzy.ysmall.ysmall_common.vo.R;
import com.atzzy.ysmall.ysmall_mapper.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author JAVAZeng
 * @Date 2022/3/3 14:17
 * @Version 1.0
 */
@Service
@Component
public class ProductServiceImpl implements ProService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private SearchMapper searchMapper;
    @Resource
    private ProductImgMapper productImgMapper;
    @Resource
    private ProductSkuMapper productSkuMapper;
    @Resource
    private ProductParamsMapper productParamsMapper;
    @Resource
    private ProductCommentsMapper productCommentsMapper;
    @Resource
    private businessInfoMapper businessInfoMapper;

    @Override
    public R listRecommendProdcuts() {

        List<Product> products = productMapper.selectRecommendProducts();
        return new R(201, "success", products);
    }

    @Override
    public R lisrGoodProducts() {
        List<Product> goodProducts = productMapper.selectGoodProducts();
        return new R(201, "success", goodProducts);
    }

    //好评推荐
    @Override
    public R listGoodProductsByImp() {
            List<Product> goodProducts = productMapper.selectGoodProducts();
            return new R(201, "success", goodProducts);
    }

    //第一次登录或者未登录 使用这个接口
    @Override
    public R listGoodProductPersonal() {
        List<Product> products = productMapper.selectGoodProductPersonal();
        return new R(201, "success", products);
    }

    //获取商品基本信息
    @Override
    public R getProductBasicInfo(Integer productId,String productBusinessName) {
        HashMap<String, Object> BaseInfo = new HashMap<>();
        //商品基本信息
        Product product = productMapper.selectProductStatus(productId);
        Integer productStatus = product.getProductStatus();
        if (productStatus == 1) {
            //商品图片
            List<ProductImgs> productImgs = productImgMapper.selectProductImgByProductId(productId);

            //商品套餐
            List<ProductSku> productSkus = productSkuMapper.selectProductSku(productId);
            List<BusinessInfos> businessInfos = businessInfoMapper.selectBuinessById(productBusinessName);
                BaseInfo.put("product", product);
                BaseInfo.put("productImg", productImgs);
                BaseInfo.put("productSku", productSkus);
                BaseInfo.put("businessInfos",businessInfos);
                return new R(201, "success", BaseInfo);


        } else {
            return new R(501, "查询商品不存在", null);
        }
    }

    //查询产品参数
    @Override
    public R getProductParamsById(String productId) {
        ProductParams productParams = productParamsMapper.selectProductParamsById(productId);
        if (productParams.getProductId() != "" && productParams.getProductId() != null) {
            return new R(201, "success", productParams);
        }
        return new R(501, "参数查询错误", null);
    }

    //查询评论详情
    @Override
    public R getProductCommentsById(String productId) {
        List<ProductCommentVo> productCommentsVos = productCommentsMapper.selelctProductComments(productId);
        return new R(201, "success", productCommentsVos);
    }

    //查询评论数量
    @Override
    public R getProductCommentNum(String productId) {
        HashMap<String, Object> comments = new HashMap<>();
        Integer total = productCommentsMapper.SumProductConmments(productId);
        Integer good = productCommentsMapper.SumGoodComments(productId);
        Integer comm = productCommentsMapper.sumCommonComments(productId);
        Integer bad = productCommentsMapper.sumBadCommentd(productId);
        comments.put("sum", total);
        comments.put("good", good);
        comments.put("comm", bad);
        comments.put("bad", comm);
        return new R(201, "success", comments);
    }
//获取商家信息
    @Override
    public R getBuinessInfo(String buinessName) {
        List<BusinessInfos> businessInfo = businessInfoMapper.selectBuinessById(buinessName);
        System.out.println(businessInfo);
        return new R(201,"success",businessInfo);
    }

    @Override
    public R getProductsByCategoryId(int categoryId, int pageNum, int limit) {
        int start = (pageNum - 1)*limit;
        List<ProductVo> productVos = productMapper.selectProductByCategoryId(categoryId, start, limit);
        int count = productMapper.selectCountByCategoryId(categoryId);
        int pageCount = count%limit==0? count/limit : count/limit+1;
        PageHelper<ProductVo> pageHelper = new PageHelper<>(count,pageCount,productVos);
        return new R(201,"success",pageHelper);
    }

    @Override
    public R listBrands(int categoryId) {
        List<String> brands = productMapper.selectBrandByCategoryId(categoryId);
        return new R(201,"success",brands);
    }

    @Override
    public R searchProduct(String keyword, int pageNum, int limit) {
        //模糊查询搜索结果
        keyword = "%" + keyword + "%";
        int start = (pageNum-1) * limit;
        List<ProductVo> productVos = productMapper.selectProductByKeyWord(keyword, start, limit);
        //查询总记录数量
        int count = productMapper.selectCountByKeyWord(keyword);

        int pageCount = count%limit == 0? count/limit:count/limit + 1;

        PageHelper<ProductVo> pageHelper = new PageHelper<>(count, pageCount, productVos);

        R success = new R(201, "success", pageHelper);

        return success;
    }

    @Override
    public R listBrands(String keyword) {
        keyword = "%" + keyword +"%";
        List<String> strings = productMapper.selectBrandByKeyword(keyword);
        return new R(201,"success",strings);
    }
    //展示商家


}
